package com.taoym.voucher.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.taoym.voucher.common.utils.HttpClientUtils;
import com.taoym.voucher.admin.commons.response.ActsResponse;
import com.taoym.voucher.admin.domain.SysActivity;
import com.taoym.voucher.admin.entity.dto.SysAddActivityDTO;
import com.taoym.voucher.admin.entity.vo.SysGetSysActivitiesVO;
import com.taoym.voucher.admin.entity.vo.SysGetUserDetailVO;
import com.taoym.voucher.admin.mapper.SysActivityMapper;
import com.taoym.voucher.admin.service.SysActivityService;
import com.taoym.voucher.admin.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @coder taoym
 * @datetime 2021/7/22 20:42
 * @desc 系统活动信息服务实现类
 */
@Service
@Slf4j
public class SysActivityServiceImpl implements SysActivityService {

    @Resource
    private SysActivityMapper sysActivityMapper;
    @Value("${jutuike.actList}")
    private String actList;
    @Autowired
    private SysUserService sysUserService;

    @Override
    public List<SysGetSysActivitiesVO> getSysActivities() {

        List<SysActivity> sysActivities = sysActivityMapper.selectList(new QueryWrapper<>());

        List<SysGetSysActivitiesVO> getSysActivitiesVOS = new ArrayList<>(sysActivities.size());
        sysActivities.forEach(e -> {
            SysGetSysActivitiesVO sysGetSysActivitiesVO = new SysGetSysActivitiesVO();
            sysGetSysActivitiesVO.setActId(e.getActId());
            sysGetSysActivitiesVO.setActName(e.getActName());
            sysGetSysActivitiesVO.setDesc(e.getDesc());
            sysGetSysActivitiesVO.setImg(e.getImg());

            getSysActivitiesVOS.add(sysGetSysActivitiesVO);
        });

        return getSysActivitiesVOS;
    }

    @Override
    public Integer addSysActivity(SysAddActivityDTO data) {

        SysActivity sysActivity = new SysActivity();
        BeanUtils.copyProperties(data, sysActivity);

        return sysActivityMapper.insert(sysActivity);
    }

    @Override
    public Integer deleteSysActivity(Integer id) {
        return sysActivityMapper.deleteById(id);
    }

    @Override
    public SysActivity actInfo(Integer actId) {
        return sysActivityMapper.selectOne(new QueryWrapper<SysActivity>().eq("act_id", actId));
    }

    @Override
    public Integer syncAct() {

        sysActivityMapper.truncateTable();
        SysGetUserDetailVO personal = sysUserService.personal();
        Map<String, Object> params = new HashMap<>();
        params.put("apikey", personal.getApiKey());
        params.put("pageSize", 100);
        String response = HttpClientUtils.doPost(actList, JSON.toJSONString(params));
        ActsResponse actsResponse = JSON.parseObject(response, ActsResponse.class);
        AtomicInteger num = new AtomicInteger();
        if (actsResponse.getCode().equals(1) && !ObjectUtils.isEmpty(actsResponse.getData())) {
            List<ActsResponse.Act> actsData = actsResponse.getData().getData();
            actsData.forEach(e -> {
                SysActivity sysActivity = new SysActivity();
                sysActivity.setActId(e.getAct_id());
                sysActivity.setActName(e.getAct_name());
                sysActivity.setImg(e.getImg());
                sysActivity.setDesc(e.getDesc());

                int i = sysActivityMapper.insert(sysActivity);
                num.set(num.get() + i);
            });
        }

        return num.get();
    }
}
